﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using System.Data;
using System.Data.SqlClient;
using Entities;

namespace DataAccessLayer
{
    public class BuoiHocDAL : DBConnection
    {
        public List<BuoiHocEntity> getAll()
        {
            List<BuoiHocEntity> lst = new List<BuoiHocEntity>();
            try
            {
                MySqlCommand cmd = new MySqlCommand("buoi_hoc_getAll", con);
                cmd.CommandType = CommandType.StoredProcedure;

                if (con.State != ConnectionState.Open)
                    con.Open();
                MySqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);

                while (dr.Read())
                {
                    BuoiHocEntity obj = new BuoiHocEntity();
                    obj.MaBuoiHoc = Convert.ToInt32(dr["MaBuoiHoc"]);
                    obj.MaKhoaHoc = Convert.ToInt32(dr["MaKhoaHoc"]);
                    obj.Ngay = Convert.ToDateTime(dr["Ngay"]);
                    obj.GhiChu = Convert.ToString(dr["GhiChu"]);
                    lst.Add(obj);
                }
                dr.Close();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                con.Close();
            }
            return lst;
        }

        public BuoiHocEntity getById(int _id)
        {
            BuoiHocEntity obj = new BuoiHocEntity();
            try
            {
                MySqlCommand cmd = new MySqlCommand("buoi_hoc_getById", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("maBuoiHoc", _id);

                if (con.State != ConnectionState.Open)
                    con.Open();
                MySqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);

                if (dr.Read())
                {
                    obj.MaBuoiHoc = Convert.ToInt32(dr["MaBuoiHoc"]);
                    obj.MaKhoaHoc = Convert.ToInt32(dr["MaKhoaHoc"]);
                    obj.Ngay = Convert.ToDateTime(dr["Ngay"]);
                    obj.GhiChu = Convert.ToString(dr["GhiChu"]);
                }
                dr.Close();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                con.Close();
            }
            return obj;
        }

        public List<BuoiHocEntity> getByKhoaHocId(int _id)
        {
            List<BuoiHocEntity> lst = new List<BuoiHocEntity>();
            try
            {
                MySqlCommand cmd = new MySqlCommand("buoi_hoc_getsByKhoaHocId", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("maKhoaHoc", _id);

                if (con.State != ConnectionState.Open)
                    con.Open();
                MySqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);

                while (dr.Read())
                {
                    BuoiHocEntity obj = new BuoiHocEntity();
                    obj.MaBuoiHoc = Convert.ToInt32(dr["MaBuoiHoc"]);
                    obj.MaKhoaHoc = Convert.ToInt32(dr["MaKhoaHoc"]);
                    obj.Ngay = Convert.ToDateTime(dr["Ngay"]);
                    obj.GhiChu = Convert.ToString(dr["GhiChu"]);
                    lst.Add(obj);
                }
                dr.Close();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                con.Close();
            }
            return lst;
        }

        public int addBuoiHoc(BuoiHocEntity _obj)
        {
            int result = 0;
            try
            {
                if (_obj.MaBuoiHoc != -1)
                    throw new Exception("Buoi Hoc Da Ton Tai");

                MySqlCommand cmd = new MySqlCommand("buoi_hoc_insert", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("maKhoaHoc", _obj.MaKhoaHoc);
                cmd.Parameters.AddWithValue("ngay", _obj.Ngay);
                cmd.Parameters.AddWithValue("ghiChu", _obj.GhiChu);
                cmd.Parameters.AddWithValue("lastId", MySqlDbType.Int32);
                cmd.Parameters["lastId"].Direction = ParameterDirection.Output;

                if (con.State != ConnectionState.Open)
                    con.Open();
                result = cmd.ExecuteNonQuery();
                int i = Convert.ToInt32(cmd.Parameters["lastId"].Value);
                if (_obj.MaBuoiHoc == -1)
                    _obj.MaBuoiHoc = i;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                con.Close();
            }
            return result;
        }

        public int deleteBuoiHoc(int _id)
        {
            int result = 0;
            try
            {
                if (_id == -1)
                    return 0;
                MySqlCommand cmd = new MySqlCommand("buoi_hoc_delete", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("maBuoiHoc", _id);
                cmd.Parameters["maBuoiHoc"].Direction = ParameterDirection.Input;

                if (con.State != ConnectionState.Open)
                    con.Open();
                result = cmd.ExecuteNonQuery();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                con.Close();
            }
            return result;
        }

        public int updateBuoiHoc(BuoiHocEntity _obj)
        {
            int result = 0;
            try
            {
                if (_obj.MaBuoiHoc   == -1)
                    return addBuoiHoc(_obj);

                MySqlCommand cmd = new MySqlCommand("buoi_hoc_update", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("maBuoiHoc", _obj.MaBuoiHoc);
                cmd.Parameters.AddWithValue("maKhoaHoc", _obj.MaKhoaHoc);
                cmd.Parameters.AddWithValue("ngay", _obj.Ngay);
                cmd.Parameters.AddWithValue("ghiChu", _obj.GhiChu);

                if (con.State != ConnectionState.Open)
                    con.Open();
                result = cmd.ExecuteNonQuery();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                con.Close();
            }
            return result;
        }
    }
}
